import glob

import cv2
import numpy as np
from pyzbar import pyzbar



def get_bar(img, debug=False):

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # ret, gray = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY)
    # gray = cv2.equalizeHist(gray)
    # a, b = 1.8, 0
    # gray = np.uint8(np.clip(gray*a + b, 0, 255))

    texts = pyzbar.decode(gray)
    if debug:
        cv2.imwrite(r'D:\code\project_honey\bar_pyzbar\t1.jpg', gray)
    tts = []
    for text in texts:
        tt = text.data.decode("utf-8")
        tts.append(tt)
        print(tt)

    return tts # [str]


def test_get_bar():
    img_path = r"D:\data\240507fubatianxian\trainV8Det_all\_add_imgs\20240509\Image__2024-05-09__10-27-04.jpg"
    img_path = r"D:\data\240507fubatianxian\trainV8Det_all\_add_imgs\20240509\Image__2024-05-09__10-27-04.jpg"
    img = cv2.imread(img_path)
    # img = cv2.imread(r'D:\data\231207huoni\trainseg_label\add_imgs\20240117\Image_20240117103210074.jpg')

    ts = get_bar(img)
    # print(ts)

    # if 1:
    #     a = 10
    # for i in range(10):
    #     s = i
    # b = 20

def test_get_bar_n():
    glob_str = r"D:\data\240507fubatianxian\trainV8Det_all\_add_imgs\20240509\*.jpg"
    ls = glob.glob(glob_str)
    for i in ls:
        print('')
        img = cv2.imread(i)
        ts = get_bar(img)
if __name__ == "__main__":

    test_get_bar_n()

